package com.alan.api;

import com.alan.api.common.ApiException;
import com.alan.api.common.ResponseBase;
import com.alan.api.common.ResponseCodeEnum;
import com.alan.api.core.ApiGatewayHand;
import com.alibaba.fastjson.JSON;
import jdk.nashorn.internal.scripts.JO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;

/**
 * @author Alan
 * @create 2018-01-12 21:40
 * @desc test
 **/
@RestController
@Slf4j
public class ApiGateWayController {

    @Autowired
    private ApiGatewayHand apiGatewayHand;

    @RequestMapping("apiGateWay")
    public ResponseBase apiGateWay(HttpServletRequest request, HttpServletResponse response){
        ResponseBase result = new ResponseBase();
        try {
            Object obj = apiGatewayHand.handle(request);
            result.setData(obj);
        } catch (ApiException e) {
            log.info("ApiException : {}",e);
            result.setEnum(ResponseCodeEnum.FAIL);
        } catch (InvocationTargetException e) {
            log.info("InvocationTargetException : {}",e);
            result.setEnum(ResponseCodeEnum.FAIL);
        } catch (IllegalAccessException e) {
            log.info("IllegalAccessException : {}",e);
            result.setEnum(ResponseCodeEnum.FAIL);
        }
        log.info("apiGateWay 返回:{}", JSON.toJSON(result));
        return result;
    }

}
